<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.goodsId"
          placeholder="请输入商品编号"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
          size="mini"
          icon="el-icon-search"
          >查找</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="addopen"
          size="mini"
          icon="el-icon-edit"
          >添加</el-button
        >
        <el-button type="primary" size="mini" icon="el-icon-download"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <div class="tables">
      <template>
        <el-table border :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="团购规则ID" align="center">
          </el-table-column>
          <el-table-column prop="goodsId" label="商品ID" align="center">
          </el-table-column>
          <el-table-column prop="goodsName" label="名称" align="center">
          </el-table-column>
          <el-table-column prop="picUrl" label="图片" align="center">
            <template slot-scope="scope">
              <img :src="scope.row.picUrl" width="50" height="50" alt="" />
            </template>
          </el-table-column>
          <el-table-column prop="discount" label="团购优惠" align="center">
          </el-table-column>
          <el-table-column
            prop="discountMember"
            label="团购要求"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="status" label="状态" align="center">
          </el-table-column>
          <el-table-column prop="expireTime" label="结束时间" align="center">
          </el-table-column>
          <el-table-column
            prop="address"
            label="操作"
            align="center"
            width="200"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
              >
                编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="dels(scope.$index, scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="get.page"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="get.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ========================= -->
    <el-dialog title="创建" :visible.sync="dialogVisible" width="50%">
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="120px"
        label-position="left"
        style="margin-left: 50px"
      >
        <el-form-item label="商品ID" prop="goodsId">
          <el-input
            v-model="form.goodsId"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item label="团购折扣" prop="discount">
          <el-input
            v-model="form.discount"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item label="团购人数要求" prop="discountMember">
          <el-input
            v-model="form.discountMember"
            style="width: 300px"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item label="过期时间" prop="expireTime">
          <el-date-picker
            v-model="form.expireTime"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button v-if="flag" type="primary" @click="update">确 定</el-button>
        <el-button v-if="!flag" type="primary" @click="addlist"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { ruleList, redact, del, add, search } from "@/api/promotion";
import moment from "moment";
export default {
  data() {
    return {
      flag: true,
      formLabelWidth: "120px",
      dialogVisible: false,
      form: {
        goodsId: "",
        discount: "",
        discountMember: "",
        expireTime: moment().format("YYYY-MM-DD HH:mm:ss"),
      },
      tableData: [],
      formInline: {},
      total: 0,
      get: {
        page: 1,
        limit: 20,
        goodsId: "",
        sort: "add_time",
        order: "desc",
      },
      rules: {
        goodsId: [{ required: true, message: "商品不能为空", trigger: "blur" }],
        discount: [
          { required: true, message: "团购折扣不能为空", trigger: "blur" },
        ],
        discountMember: [
          { required: true, message: "团购人数不能为空", trigger: "blur" },
        ],
        expireTime: [
          { required: true, message: "过期时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getdata();
  },
  methods: {
    getdata() {
      ruleList(this.get).then((res) => {
        console.log(res);
        if ((res.data.errmsg = "成功")) {
          this.tableData = res.data.list;
          this.total = res.data.total;
          console.log(this.tableData);
        }
      });
    },
    // 查询
    onSubmit() {
      this.get.page = 1;
      this.get.goodsId = this.formInline.goodsId;
      this.getdata();
    },

    // 编辑
    handleEdit(a, b) {
      this.flag = true;
      this.dialogVisible = true;
      this.form = b;
    },
    //编辑确定
    update() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          redact(this.form).then((res) => {
            if (res.errno == 652) {
              console.log(res);
              this.getdata();
              this.form = {};
              this.dialogVisible = false;
              if (res.errno == 0) {
                this.$notify({
                  title: "成功",
                  message: "更新团购规则成功",
                  type: "success",
                });
              } else {
                this.$notify({
                  title: "失败",
                  message: "团购商品不存在",
                  type: "error",
                });
              }
            }
          });
        }
      });
    },
    close() {
      this.dialogVisible = false;
      this.form = {};
    },
    // 删除
    dels(a, b) {
      del(b).then((res) => {
        if (res.errmsg == "成功") {
          this.getdata();
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    // 添加打开
    addopen() {
      this.flag = false;
      this.dialogVisible = true;
    },
    //添加
    addlist() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          add(this.form).then((res) => {
            console.log(res);
            this.getdata();
            this.form = {};
            this.dialogVisible = false;
            if (res.errno == 0) {
              this.$notify({
                title: "成功",
                message: "更新团购规则成功",
                type: "success",
              });
            } else if(res.errno == 651) {
              this.$notify({
                title: "失败",
                message: "团购商品已存在",
                type: "error",
              });
            }else{
              this.$notify({
                title: "失败",
                message: "团购商品不存在",
                type: "error",
              });
            }
          });
        }
      });
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.get.limit = val;
      this.getdata();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.get.page = val;
      this.getdata();
    },
    formatDate(row, column, cellValue) {
      return moment(cellValue).format("YYYY-MM-DD HH:mm:ss");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table td,
::v-deep .el-table th {
  padding: 4px 0px;
}
.el-table {
  font-size: 12px;
}
.page {
  margin-top: 40px;
  display: flex;
  justify-content: left;
}
::v-deep.el-form-item label {
  width: 200px;
}
</style>
